package com.kakavr.emp.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Maps;
import com.kakavr.auth.util.EncryptionUtils;
import com.kakavr.base.controller.BaseController;
import com.kakavr.base.exception.ValidateParamException;
import com.kakavr.result.MsgResult;
import com.kakavr.result.constant.MsgConstants;
import com.kakavr.shop.service.IShopAdminService;
import com.kakavr.util.SessionUtils;
import com.kakavr.util.ValidatorUtils;
import com.kakavr.work.service.IWorkRecordService;
/**
 * 店员信息
 * @author
 *
 */
@RestController
@RequestMapping("emp")
public class EmployeeController extends BaseController {
	
	@Resource
	private IShopAdminService shopAdminService;
	@Resource
	private IWorkRecordService workRecordService;
	
	/**
	 * 修改密码
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "updatePass", method = RequestMethod.POST)
	public Map<String, Object> updatePass(HttpServletRequest request) {
		String oldPass = request.getParameter("oldPass");
		if (StringUtils.isBlank(oldPass)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "原密码");
		}
		String password = request.getParameter("password");
		if (StringUtils.isBlank(password)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "密码");
		}
		if (!ValidatorUtils.isPass(password)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_2004,
					MsgConstants.MSG_CONTENT_2004, "密码");
		}
		Map<String, Object> user = SessionUtils.getCurUser();
		String pass = (String) user.get("password");
		if (oldPass.equals(pass) || EncryptionUtils.md5(oldPass).equals(pass)){
			Map<String, Object> map = Maps.newHashMap();
			map.put("empPass", EncryptionUtils.md5(password));
			map.put("empId", SessionUtils.getUserId());
			map.put("isUpPass", 1);
			shopAdminService.updateEmployee(map);
			return MsgResult.okResult();
		}
		throw new ValidateParamException(MsgConstants.MSG_CODE_1002,
				MsgConstants.MSG_CONTENT_1002, "原密码");
	}
	
	/**
	 * 统计提成
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "staticCommis", method = RequestMethod.GET)
	public Map<String, Object> staticCommission(HttpServletRequest request){
		Map<String, Object> map = Maps.newHashMap();
		map.put("begin", request.getParameter("begin"));
		map.put("end", request.getParameter("end"));
		map.put("type", request.getParameter("type"));
		map.put("empId", SessionUtils.getUserId());
		return MsgResult.okResultData(workRecordService.staticWorkByOwn(map));
	}

}
